package com.imooc.controller;

import com.imooc.service.UserPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/perm")
public class UserPermController {

    @Autowired
    private UserPermissionService userPermissionService;
    
    /**
     * 检查用户是否有权限
     * @param userId 用户ID
     * @param resource 资源
     * @param action 操作
     * @return 权限检查结果
     */
    @GetMapping("/check")
    public ResponseEntity<Map<String, Object>> checkPermission(
            @RequestParam("userId") String userId,
            @RequestParam("resource") String resource,
            @RequestParam("action") String action) {
        boolean hasPermission = userPermissionService.checkPermission(userId, resource, action);
        Map<String, Object> result = new HashMap<>();
        result.put("hasPermission", hasPermission);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 为用户添加权限
     * @param userId 用户ID
     * @param resource 资源
     * @param action 操作
     * @return 操作结果
     */
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addPermission(
            @RequestParam("userId") String userId,
            @RequestParam("resource") String resource,
            @RequestParam("action") String action) {
        boolean success = userPermissionService.addPolicy(userId, resource, action);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 移除用户权限
     * @param userId 用户ID
     * @param resource 资源
     * @param action 操作
     * @return 操作结果
     */
    @DeleteMapping("/remove")
    public ResponseEntity<Map<String, Object>> removePermission(
            @RequestParam("userId") String userId,
            @RequestParam("resource") String resource,
            @RequestParam("action") String action) {
        boolean success = userPermissionService.removePolicy(userId, resource, action);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取用户的所有权限
     * @param userId 用户ID
     * @return 权限列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<List<String>>> getUserPermissions(@PathVariable("userId") String userId) {
        List<List<String>> permissions = userPermissionService.getPermissionsForUser(userId);
        return ResponseEntity.ok(permissions);
    }
    
    /**
     * 获取所有权限策略
     * @return 所有权限策略
     */
    @GetMapping("/all")
    public ResponseEntity<List<List<String>>> getAllPolicies() {
        List<List<String>> policies = userPermissionService.getAllPolicies();
        return ResponseEntity.ok(policies);
    }
    
    /**
     * 为用户添加角色
     * @param userId 用户ID
     * @param role 角色
     * @return 操作结果
     */
    @PostMapping("/role/add")
    public ResponseEntity<Map<String, Object>> addRoleForUser(
            @RequestParam("userId") String userId,
            @RequestParam("role") String role) {
        boolean success = userPermissionService.addRoleForUser(userId, role);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 获取用户的所有角色
     * @param userId 用户ID
     * @return 角色列表
     */
    @GetMapping("/role/user/{userId}")
    public ResponseEntity<List<String>> getUserRoles(@PathVariable("userId") String userId) {
        List<String> roles = userPermissionService.getRolesForUser(userId);
        return ResponseEntity.ok(roles);
    }
    
    /**
     * 删除用户的角色
     * @param userId 用户ID
     * @param role 角色
     * @return 操作结果
     */
    @DeleteMapping("/role/remove")
    public ResponseEntity<Map<String, Object>> deleteRoleForUser(
            @RequestParam("userId") String userId,
            @RequestParam("role") String role) {
        boolean success = userPermissionService.deleteRoleForUser(userId, role);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 删除用户的所有权限
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/user/{userId}")
    public ResponseEntity<Map<String, Object>> deletePermissionsForUser(@PathVariable("userId") String userId) {
        boolean success = userPermissionService.deletePermissionsForUser(userId);
        Map<String, Object> result = new HashMap<>();
        result.put("success", success);
        return ResponseEntity.ok(result);
    }
}
